<?php
namespace Qgs\Controller;
use Qgs\Controller\BaseController;
use Think\Controller;
class WeixinController extends BaseController {

    public $local_id;

    public function __construct()
    {
        parent::__construct();
        $this->_arr['title'] = '微信设置';
        $this->local_id = session('userInfo')['local_id'];
    }
    public function index(){
        $this->_arr['title_two'] = '微信模板列表';
        $this->_arr['p'] = I('p', 1, 'intval');
        $where = 'a.is_terrace = 0';
        if($this->_arr['title_name']){
            $where .= " and title_name like '%".$this->_arr['title_name']."%''";
        }
        $sl_id = I('id') ? I('id') : 2;
        if ($sl_id == 1) {
            $order = 'a.wx_id asc';
            $this->_arr['rank'] = 2;
        } elseif ($sl_id == 2) {
            $order = 'a.wx_id desc';
            $this->_arr['rank'] = 1;
        }
        //dump($where);die;store_wx_information
        $data = M('wx_information a') ->field('a.*,IF(b.status, b.status, 0) as status')
            ->join(C('DB_PREFIX') . "store_wx_information b on a.wx_id=b.wx_id and b.`local_id` = ".$this->local_id, 'left')
            ->where($where)
            ->order($order)
            ->page($this->_arr['p'] . ','.$this->page_num)
            ->select();
        //echo M()->_sql();
        //dump($data);die;
        $count = M('wx_information')->where($where)->count();// 查询满足要求的总记录数
        //dump($count);
        $Page = new \Think\Page($count, $this->page_num);// 实例化分页类 传入总记录数和每页显示的记录数
        $show = $Page->show();// 分页显示输出
        //dump($show);
        $this->_arr['page'] = $show;// 赋值分页输出
        $this->_arr['list'] = $data;
        $this->_showDisplay();
    }

    /**
     * 启用（禁用）
     */
    public function freezeTemplate(){
        $wx_id = I('id') or exit(json_encode(['stat' => false,'msg' => '参数错误']));
        $status = I('status');
        if($status == 2){
            $status = 1;
        }else{
            $status = 2;
        }

        if(M('store_wx_information')->where(['wx_id' => $wx_id,'local_id'=>$this->local_id])->find()){
            $sat = M('store_wx_information')->where(['wx_id' => $wx_id,'local_id'=>$this->local_id])->save(['status'=>$status]);
            //dump($sat);die;
            exit(json_encode($sat === false ? ['stat' => false,'msg' => '失败'] : ['stat' => true,'msg' => '成功']));
        }
        //读取信息模板
        $add_arr = [
            'wx_id' => $wx_id,
            'local_id' => $this->local_id,
            'status' => $status,
        ];
        $sat = M('store_wx_information')->add($add_arr);
        exit(json_encode($sat === false ? ['stat' => false,'msg' => '失败'] : ['stat' => true,'msg' => '成功']));
    }

    /**
     * 新增微信消息模板
     */
    public function newTemplate(){
        $this->_showDisplay();
    }
    public function doNewTemplate(){
        $obj = new \Home\Event\WeixinEvent();
        $data['number'] = I('number') or $this->error('编号不能为空');
        $data['title_name'] = I('title_name') or $this->error('模板标题不能为空');
        $data['create_time'] = time();
        //请求微信获取模板ID
        $acquire = $obj->acquireTemplate($data['number']);
        if(!$acquire['template_id']){
            $this->error('未请求到模板ID');
        }
        $data['template_id'] = $acquire['template_id'];
        $status = M('wx_information')->add($data);
        $status === false ? $this->error('失败') : $this->success('配置成功', 'wxInformation');
    }


  

    

   

}